<?php

namespace StudyBuddy\Controllers;

use \StudyBuddy\SubmittedUploadresourceWWW;
use \StudyBuddy\WebPage;
use \StudyBuddy\UploadresourceParser;
use \StudyBuddy\String;
use \StudyBuddy\Request;
use \StudyBuddy\Responder;

class Uploadresource extends WebPage {

    protected $permission = 'uploadresource';
    protected $useToken = false;

    const SUBJECT = 'Your %s login information';

    /**
     * Message to show upon completed successful
     * uploadresource
     * 
     * @var string
     */
    const SUCCESS = 'You upload resource success!';

    protected $layoutID = 1;

    /**
     * Object Uploadresourceform;
     *
     * @var object Forms\Regform
     */
    protected $oForm;

    protected function main() {
        $this->aPageVars['title'] = $this->_('Create New Resource');
        /**
         * Don't bother with token
         * for this form.
         * It uses captcha, so allow
         * users to submit without token
         */
        $this->oForm = new \StudyBuddy\Forms\Uploadresourceform($this->oRegistry, false);
        /**
         * Set divID to registration because otherwise
         * it is default to 'regform' which causes
         * the whole form's div to be turned into
         * a modal which is used in quickReg or Join controllers
         * but for this controller we want a regular web page,
         * no modals, no Ajax
         *
         * Also set className to 'registration' because it defaults
         * to yui-pre-content which makes the whole div hidden
         * This is a trick for adding something that later is turned
         * into modal, but we don't need it for this page
         */
        if ($this->oForm->isSubmitted()) {
            if ($this->oForm->validate()) {
                $this->createNewResource()->showSuccess();
            } else {
                $this->showFormWithErrors();
            }
        } else {
            if ($this->useToken) {
                $this->oForm->token = self::generateToken();
            }
            $this->aPageVars['body'] = '<div id="userForm" class="frm1">' . $this->oForm->getForm() . '</div>';
        }
    }

    protected function showFormWithErrors() {
        if ($this->useToken) {
            $this->oForm->token = self::generateToken();
        }
        /**
         * In case of Ajax can just return the form now
         */
        $this->aPageVars['body'] = '<div id="userForm" class="frm1">' . $this->oForm->getForm() . '</div>';

        return $this;
    }
    
    protected function showSuccess(){
        $this->aPageVars['body'] = '<div id="userForm" class="frm1">' . self::SUCCESS . '</div>';

        return $this;
    }

    /**
     *
     * Create new record in USERS collection,
     *
     * @return object $this
     */
    protected function createNewResource() {

        UploadresourceParser::factory($this->oRegistry)->save(new SubmittedUploadresourceWWW($this->oForm));

        return $this;
    }

    /**
     * Generate unique ID and store in session
     * The page will have the meta tag 'version'
     * with the value of this token
     * it will be used by ajax based forms when submitting
     * form via ajax
     *
     *
     * @return string value of form token
     * for this class.
     */
    public static function generateToken() {
        if (!array_key_exists('secret', $_SESSION)) {

            $token = uniqid(mt_rand());
            //$_SESSION['secret'] = $token;
            $_SESSION['secret'] = hash('md5', $token);
        }

        return $_SESSION['secret'];
        //return hash('md5', $_SESSION['secret'].get_called_class());
    }

}
